package com.qf.springMVCUserRegisterLoginDemo.controller;

import com.qf.springMVCUserRegisterLoginDemo.entity.User;
import com.qf.springMVCUserRegisterLoginDemo.entity.UserVo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 刘欣
 * @createTime 2023/1/7 18:30
 * @description
 */
@RestController
public class UserController {

    private Map<String, User> map = new HashMap<>();

    @RequestMapping(value = "/userLogin", produces = "text/json;charset=UTF-8")
    public String login(UserVo userVo, HttpSession session) {
        String captcha = (String) session.getAttribute("captcha");
        String code = userVo.getCode();
        if (code == "") {
            return "-2";
        }
        if (captcha.equals(code)) {
            User user = new User();
            user.setUsername(userVo.getUsername());
            user.setPassword(userVo.getPassword());
            user.setPhone(userVo.getPhone());
            map.put(user.getUsername(), user);
            return "1";
        } else {
            return "-1";
        }
    }

    @RequestMapping(value = "/checkName", produces = "text/json;charset=UTF-8")
    public String checkName(@RequestBody String username) {
        if ("username=".equals(username)) {
            return "3";
        } else {
            String name = username.split("=")[1];
            String msg = "1";
            for (Map.Entry<String, User> entry : map.entrySet()) {
                String key = entry.getKey();
                if (name.equals(entry.getKey())) {
                    msg = "2";
                    break;
                }
            }
            return msg;
        }
    }
}
